home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992…tember: A ROM With a View / devSep92 / devSep92.dmg / Tools & Apps / OS⁄Toolbox / Threads Package / Threads.h < prev    next >
Encoding:
C/C++ Source or Header  |  1991-03-15  |  5.6 KB  |  167 lines  |  [TEXT/MPS ]

  1. #ifndef __THREADS__
  2. #define __THREADS__
  3.  
  4. // by undefining inline, this C++ source can be compiled with no modifications
  5. // well...maybe
  6. #define inline
  7.  
  8. /*  Copyright © 1989-91 by Apple Computer, Inc.  All rights reserved.    */
  9.  
  10. #include <Types.h>
  11.  
  12. /* Each thing can be a member of a list, and can also contain a list of other things. */
  13.  
  14. typedef struct Thing**            ThingHandle;
  15. typedef struct Thing*            ThingPtr;
  16.  
  17. struct Thing 
  18.     {
  19.     ThingHandle        fQueue;                // the containing “Thing list” this thing is on 
  20.     ThingHandle        fNext;                // the next thing in the list relative to this thing
  21.     ThingHandle        fPrev;                // the previous thing in the list relative to this thing
  22.     ThingHandle        fHead;                // the first thing in the list of things owned by this thing
  23.     ThingHandle        fTail;                // the last thing in the list of things owned by this thing
  24.     };
  25.  
  26.  
  27.  
  28.  
  29.  
  30. typedef struct Thread**            ThreadHandle;
  31. typedef struct ThreadQueue**    ThreadQueueHandle;
  32. typedef struct Semaphore**        SemaphoreHandle;
  33. typedef struct Thread*            ThreadPtr;
  34. typedef struct ThreadQueue*        ThreadQueuePtr;
  35.  
  36. typedef pascal void (*ThreadProc)(ThreadHandle);
  37. typedef pascal ThreadHandle (*ScheduleProc)(ThreadHandle);
  38.  
  39.  
  40. typedef long ThreadError;
  41.  
  42. #define eNoError             0        // no error
  43. #define eOutOfMemory       -1        // memory allocation failure
  44. // more errors will be reported
  45.  
  46. extern ThreadError     gThreadError;            // set by thread routines
  47. extern Boolean        gSystemLocked;
  48.  
  49. typedef long ThreadType;
  50.  
  51. #define    StackCopy            0
  52. //const ThreadType    HeapStack    =    ;        // unimplemented
  53.  
  54. typedef long ThreadState;
  55.  
  56. #define    running            0         // the Thread is the active context
  57. #define    pending            1        // the Thread is awaiting scheduleing (ie. on gPendingQueue)
  58. #define    blocked            2        // the Thread is on a semaphore
  59. #define    sleeping        3        // the Thread is not blocked or pending
  60. #define    ended            4        // the Thread is about to be free'd
  61.  
  62.  
  63. struct Thread {
  64.     struct Thing        fThing;
  65.     ThreadType            fType;
  66.     ThreadState            fState;
  67.     Boolean                fLocked;            // if the ThreadHandle and fStack are Locked - avoids copious calls to HLock/Hunlock
  68.     Handle                fStack;                // the storage for the stack data - if ThreadType == HeapStack this is HLock'ed
  69.  
  70.     ThreadProc            fCopyContext;        // copy current context - store in fStack
  71.     ThreadProc            fSwapIn;            // called to context switch "this" thread in
  72.     ThreadProc            fSwapOut;            // calls fSchedule then fSwapIn on the nextThread
  73.     ThreadProc            fFree;                // called to dispose of the Thread
  74.     ScheduleProc        fSchedule;            // queue this thread (if necessary), return the next one
  75.  
  76.     long                fUserBytes[8];        // for user use
  77.     };
  78.  
  79. typedef struct Thread Thread;
  80.  
  81.  
  82. // manager routines:
  83.  
  84. pascal void            InitThreads(ThreadHandle mainThread, Boolean usesFPU);    // if the "mainThread" requires special methods, create it - don't start it and pass it in
  85. pascal ThreadHandle    GetCurrentThread();
  86. pascal void            Yield();
  87. pascal void            ExitThreads();                            // while gPendingQueue is not empty Yield
  88.  
  89. // thread routines:
  90.  
  91. #define kDefaultStackSize  1024
  92.  
  93. pascal ThreadHandle    NewThread(long stackSize /* ThreadType t */);
  94. pascal void            StartThread(ThreadHandle theThread);    // call fCopy proc and Wake
  95. pascal Boolean        InThread(ThreadHandle theThread);        // true if theThread is running
  96. pascal void            Sleep(ThreadHandle theThread);            // remove form gPendingQueue (or swap out if running) place on gSleepingQueue
  97. pascal void            Wake(ThreadHandle theThread);            // place on gPendingQueue
  98. pascal void            EndThread(ThreadHandle theThread);        // call fFree - never returns, end of context
  99. pascal ThreadHandle Spawn(ThreadHandle, pascal void (*threadProc)(ThreadHandle, long), long stackSize, long refcon);
  100.  
  101. // convienence routine:
  102.  
  103. pascal Boolean        InNewThread(ThreadHandle* theThread, long stackSize);    // NewThread, StartThread, InThread
  104.  
  105.  
  106. // default methods:
  107.  
  108. pascal void                TCopyContext(ThreadHandle);    // copy the current context
  109. pascal void                TSwapIn(ThreadHandle);        // make this thread's context the active context        
  110. pascal void                TSwapOut(ThreadHandle);        // save the current context (or free the thread if  fState == ended)
  111. pascal void                TFree(ThreadHandle);        // dispose of the thread
  112. pascal ThreadHandle        TSchedule(ThreadHandle);     // returns the next thread to be run, queue this thread is necessary
  113.  
  114.  
  115. // semaphores:
  116.  
  117.  
  118. // semaphores:
  119.  
  120. struct Semaphore 
  121.     {
  122.     struct Thing        fThing;
  123.     long                fCount;
  124.     };
  125. typedef struct Semaphore Semaphore;
  126.  
  127. pascal SemaphoreHandle    NewSemaphore();
  128. pascal void                BlockOnSemaphore(SemaphoreHandle, ThreadHandle);
  129. pascal void                ReleaseOneThread(SemaphoreHandle);
  130. pascal void                ReleaseAllThreads(SemaphoreHandle);
  131. pascal void                FreeSemaphore(SemaphoreHandle);
  132.  
  133. pascal void             GrabSemaphore(SemaphoreHandle);
  134. pascal void             ReleaseSemaphore(SemaphoreHandle);
  135.  
  136.  
  137.  
  138.  
  139. // ThreadQueue:
  140.  
  141. extern ThreadQueueHandle    gPendingThreads;            // holds pending threads
  142.  
  143. struct ThreadQueue 
  144.     {
  145.     struct Thing        fThing;
  146.     };
  147.     
  148. typedef struct ThreadQueue ThreadQueue;
  149.  
  150. pascal ThreadQueueHandle    NewThreadQueue();
  151. pascal void                    FreeThreadQueue(ThreadQueueHandle);    // calls EndThread on any queued threads
  152.  
  153. pascal void                    IThing(ThingHandle);
  154.  
  155. pascal void                    ThingInsertLast(ThingHandle queue, ThingHandle thing);
  156. pascal ThingHandle            ThingTakeFirst(ThingHandle);
  157. pascal ThingHandle            ThingGetFirst(ThingHandle);
  158. pascal void                    ThingRemove(ThingHandle thing);
  159.  
  160. pascal ThingHandle            ThingOwner(ThingHandle);                                    // return the queue this thing is on
  161. pascal Boolean                ThingIsEmpty(ThingHandle);                                    // indicate whether or not this thing contains one or more things
  162. typedef pascal Boolean         (*ThingIterProc)(ThingHandle, void*);
  163. pascal void                    ThingEach(ThingHandle, ThingIterProc, void* userPtr);        // terminates if ThingIterProc returns true
  164.  
  165. pascal void                    FreeThing(ThingHandle);                                        // lets go of contained things, but does not free them, then frees self
  166.  
  167. #endif